home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 575 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.8 KB  |  49 lines

  1. Path: news.iag.net!news
  2. From: jatmon@iag.net (John R Buchan)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Q: Terminating program at EOF
  5. Date: 7 Jan 1996 06:22:00 GMT
  6. Organization: Internet Access Group, Orlando, Florida
  7. Message-ID: <4cnoq8$4fv@news.iag.net>
  8. References: <4cn66j$5r0@fnpx20.fnal.gov>
  9. NNTP-Posting-Host: pm2-orl7.iag.net
  10. X-Newsreader: WinVN 0.99.7
  11.  
  12. In article <4cn66j$5r0@fnpx20.fnal.gov>, sfield@fnpx20.fnal.gov says...
  13. <snip>
  14. >I've written a program that does the job, but it doesn't stop when it reaches
  15. >the end of the file. The program will write out the reformatted file, but it
  16. >appends a lot of "extra" characters to the end of the file and only stops 
  17. when
  18. >I break out of it.
  19. <snip>
  20. >#include <stdio.h>
  21. >#define MAX_WORD_LENGTH 80      /* max length of word in input */
  22. >
  23. >main()
  24. >{
  25. >  int line_length,              /* max length of line in output */
  26. >      curr_line_length = 0,     /* # of chars printed so far in curr line */
  27. >      word_length;              /* length of current word */
  28. >  char word[ MAX_WORD_LENGTH+1];/* buffer to read word +1 for terminating
  29. >                                    '\0' */
  30. >  char c_old=' ',c_new=' ';     /* check for newlines with chars */
  31. <snip>
  32. >
  33. >/*  while((c_new=fgetc(fptr_read))!=EOF){ */ /* old method for removing EOF 
  34. bug */
  35. >    while(c_new!=EOF){                       /* new method, still doesn't 
  36. work */
  37.  
  38. There is absolutely no guarantee that EOF can be represented by a char.  This
  39. is why the ansi character i/o functions use int instead of char (check the
  40. declaration/prototype for fgetc). Try changing c_new to int and see, if the
  41. problem clears up.
  42.  
  43. PS) you need to add a return statement to the end of main.
  44.  
  45. -- 
  46. John R Buchan           -:|:-     Looking for that elusive FAQ?  ftp to:
  47. jatmon@mail.iag.net     -:|:-     rtfm.mit.edu /pub/usenet-by-group/....
  48.  
  49.